home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Appletalk Classes / CPPConfirmTask.cp next >
Encoding:
Text File  |  1996-04-04  |  5.2 KB  |  181 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/17/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPConfirmTask
  6.     
  7.     SUPERCLASS: CPPPeriodicTask
  8.     
  9.         This class lets you asynchronously confirm the address of
  10.         a node on the network
  11.     
  12. ********************************************************************/
  13.  
  14. #include    <CPPConfirmTask.h>
  15. #include    <CPPTaskManager.h>
  16. #include    <CPPNodeInfo.h>
  17. #include    <MemoryTools.h>
  18.  
  19.  
  20. /*-----------------------------------------------------------------*/
  21. /*------------------------ PUBLIC METHODS -------------------------*/
  22. /*-----------------------------------------------------------------*/
  23.  
  24.     CPPConfirmTask::CPPConfirmTask (CPPTaskManager *TaskManager, 
  25.                                     long minPeriod, 
  26.                                     Boolean deleteWhenDone) :
  27.                                       CPPPeriodicTask (TaskManager, minPeriod,
  28.                                                      deleteWhenDone)
  29.     {
  30.         this->confirmRec = NULL;
  31.         this->confirmAddr = NULL;
  32.         this->NodeToConfirm = NULL;
  33.         this->NTE = NULL;
  34.         this->foundIt = NULL;
  35.     }
  36.     
  37. /*-----------------------------------------------------------------*/
  38.     
  39.     CPPConfirmTask::~CPPConfirmTask (void)
  40.     {
  41.         MPPParamBlock     killPB;
  42.         OSErr             ErrCode;
  43.     
  44.         if (this->confirmRec)
  45.         // cancel the confirm call, if there is one pending
  46.           if (this->confirmRec->MPPioResult == 1) 
  47.             {
  48.               killPB.NBPnKillQEl = (Ptr)&confirmRec->MPP.qLink;
  49.               ErrCode = PKillNBP(&killPB, false);
  50.             }        
  51.         
  52.         if (this->NodeToConfirm)
  53.           delete this->NodeToConfirm;
  54.         NukePtr(this->confirmRec);
  55.         NukePtr(this->confirmAddr);
  56.         NukePtr(this->NTE);
  57.     }
  58.  
  59. /*-----------------------------------------------------------------*/
  60.  
  61.     char *CPPConfirmTask::ClassName (void)
  62.     {
  63.         return "CPPConfirmTask";
  64.     }
  65.  
  66. /*-----------------------------------------------------------------*/
  67.  
  68.     void CPPConfirmTask::StartNodeConfirm (CPPNodeInfo *TheNode,
  69.                                             CompletionProc DoProc)
  70.     /* this routine sets all the initial values for the lookup task */
  71.     /* and then sends it to the manager to be queued for execution */
  72.     {
  73.         // exit if this task is still involved in a lookup
  74.         // our have no manager
  75.         if ((!this->hasCompleted) || (!this->ourManager))
  76.           return;
  77.                 
  78.         // otherwise, set up the call
  79.         // first allocate the structures we will use
  80.         this->confirmRec = (MPPParamBlock *)NewPtrClear(sizeof(MPPParamBlock));
  81.         if ((this->callResult = MemError()) != noErr) return;
  82.         
  83.         this->NTE = (NamesTableEntry *)NewPtrClear(sizeof(NamesTableEntry));
  84.         if ((this->callResult = MemError()) != noErr) return;
  85.  
  86.         this->confirmAddr = (AddrBlock *)NewPtrClear(sizeof(AddrBlock));
  87.         if ((this->callResult = MemError()) != noErr) return;
  88.  
  89.         if (this->NodeToConfirm)
  90.           delete this->NodeToConfirm;
  91.         
  92.         this->NodeToConfirm = (CPPNodeInfo *)TheNode->Clone();
  93.         this->SetCompletionProc(DoProc);
  94.         this->hasCompleted = FALSE;            // note that we are not done
  95.         
  96.         SetupConfirmCall(TheNode);
  97.         
  98.         if (this->callResult == noErr)
  99.           this->ourManager->AddPeriodicTask(this);    // now add the task
  100.     }
  101.     
  102. /*-----------------------------------------------------------------*/
  103.  
  104.     void CPPConfirmTask::DoPeriodicAction (void)
  105.     /* if the lookup call has completed, extract the nodes and */
  106.     /* execute another lookup call, unless we have gotten all the nodes */
  107.     /* in which case, set the hasCompleted flag */
  108.     {
  109.         // call the inherited method to update frequency count
  110.         CPPPeriodicTask::DoPeriodicAction();
  111.         
  112.         // do our periodic task
  113.         switch (this->confirmRec->MPPioResult) {
  114.             case noErr :    // the call has completed
  115.                 this->foundIt = TRUE;
  116.                 this->hasCompleted = TRUE;
  117.                 this->callResult = noErr;
  118.                 break;
  119.                 
  120.             case 1  :        // still busy finding the name
  121.                 break;
  122.                 
  123.             default    :        // an error occurred
  124.                 this->callResult = confirmRec->MPPioResult;
  125.                 this->foundIt = FALSE;
  126.                 hasCompleted = TRUE;
  127.                 break;
  128.         }
  129.         
  130.     }
  131.  
  132. /*-----------------------------------------------------------------*/
  133.  
  134.     void CPPConfirmTask::DoCompletedAction(void)
  135.     /* delete the structures we allocated to do the lookup */
  136.     {
  137.         NukePtr(this->confirmRec);
  138.         NukePtr(this->confirmAddr);
  139.         NukePtr(this->NTE);
  140.     
  141.         CPPPeriodicTask::DoCompletedAction();
  142.     }
  143.  
  144. /*-----------------------------------------------------------------*/
  145.  
  146.       Boolean    CPPConfirmTask::NodeExists (Boolean *isDone)
  147.       {
  148.           return this->foundIt;
  149.       }
  150.  
  151. /*-----------------------------------------------------------------*/
  152. /*---------------------- PROTECTED MEMEBERS -----------------------*/
  153. /*-----------------------------------------------------------------*/
  154.  
  155.     void CPPConfirmTask::SetupConfirmCall (CPPNodeInfo *TheNode)
  156.     {
  157.         StringPtr    ObjStr, TypeStr, ZoneStr;
  158.         short        SNum, NNum, ZNum;
  159.         
  160.         // extract the address of the node we want to confirm
  161.         TheNode->GetNodeAddress (&SNum, &NNum, &ZNum);
  162.         confirmAddr->aSocket = SNum; 
  163.         confirmAddr->aNode = NNum; 
  164.         (short)confirmAddr->aNet = ZNum;
  165.         
  166.         // construct the name we want to confirm
  167.         TheNode->GetNodeName (&ObjStr, &TypeStr, &ZoneStr);
  168.         NBPSetEntity((Ptr)NTE->nt.entityData, (Ptr)ObjStr, 
  169.                      (Ptr)TypeStr, (Ptr)ZoneStr);
  170.         
  171.         // set up the call record
  172.         confirmRec->MPPioCompletion = NULL;
  173.         confirmRec->NBPentityPtr = (Ptr)&NTE->nt.entityData;
  174.         confirmRec->NBPinterval = 2;
  175.         confirmRec->NBPcount = 3;
  176.         confirmRec->NBPconfirmAddr = *confirmAddr;
  177.         
  178.         // make the call synchronously
  179.         this->callResult = PConfirmName(confirmRec,TRUE);
  180.     }
  181.